<?php

class Grades extends Controller
{
	function Grades()
	{
		parent::Controller();
		$this->load->helper('form');
		$this->load->helper('url');
        $this->load->helper('email');
	}
	
	
	function index()
	{
		
		if(NULL ==  getenv('HTTP_REFERER'))
		{
			echo "NASTĄPIŁA PRÓBA WYŁUDZENIA INFORMACJI !";
		}
		else
		{
			$this->load->helper('form');		
			$this->load->view("grades_start_view");	
		}
	}
	
	
	function showgrade()
	{
		
		$imie  = $this->input->post('imie');
		$nazwisko  = $this->input->post('nazwisko');
		$subject  = $this->input->post('przedmiot');
		$class  = $this->input->post('klasa');
		
		$query = "SELECT COUNT(s.id) AS luczniow FROM students s, classes c WHERE s.classe_id = c.id AND c.name = '" . $class ."'";
		$query = $this->db->query($query);
		
		foreach ($query->result() as $row)
    	{
    		$luczniow = $row->luczniow;
        }
        
        
        $query = "SELECT COUNT(d.id) AS locen FROM classes c, subjects s, subjects_teachers st, descriptions d, teachers t
					WHERE c.id = st.classe_id AND s.id = st.subject_id AND s.id = d.subject_id AND st.teacher_id = t.id
					AND t.firstname = '" . $imie. "' AND t.secondname = '" . $nazwisko. "' AND s.name = '" . $subject . "' AND c.name = '" . $class . "'";		
		$query = $this->db->query($query);
		
		foreach ($query->result() as $row)
    	{
    		$locen = $row->locen;
        }
        
					
		$query = "SELECT d.description AS opis, d.weight AS waga, d.id FROM classes c, students s, grades g, descriptions d, teachers t, subjects su
				WHERE c.id = s.classe_id AND g.student_id = s.id AND g.description_id = d.id AND g.teacher_id = t.id AND su.id = d.subject_id
				AND t.firstname = '" . $imie ."' AND t.secondname = '" . $nazwisko . "' AND su.name = '" . $subject . "' AND c.name = '" . $class . "' GROUP BY d.id ORDER BY d.description ASC";
		$powody = $this->db->query($query);
				
			
		$query = "SELECT s.secondname AS nazwisko , s.firstname AS imie, d.description AS opis, g.grade AS ocena, d.weight AS waga, g.id FROM classes c, students s, grades g, descriptions d, subjects su
					WHERE c.id = s.classe_id AND s.id = g.student_id AND g.description_id = d.id AND d.subject_id = su.id AND c.name = '" . $class . "'
					AND su.name = '" . $subject . "' ORDER BY s.secondname, s.firstname, d.description";	
		$oceny = $this->db->query($query);
		
		
		$query = "SELECT s.secondname AS nazwisko, s.firstname AS imie, s.id AS id FROM students s, classes c WHERE  s.classe_id = c.id AND c.name = '" . $class . "' ORDER BY s.secondname, s.firstname";
		$spisuczniow = $this->db->query($query);
		
		
        $tablica = array('przedmiot' => $subject, 'klasa' => $class, 'imie' => $imie, 'nazwisko' => $nazwisko, 'luczniow' => $luczniow, 'locen' => $locen,
        				'powody' => $powody, 'oceny' => $oceny, 'spisuczniow' => $spisuczniow);
        $this->load->view("grades_view", $tablica);
        
	}	
	
	
	
	function addgradedescription($subject, $class, $imie, $nazwisko)
	{		
		
		if(NULL ==  getenv('HTTP_REFERER'))
		{
			echo "NASTĄPIŁA PRÓBA WYŁUDZENIA INFORMACJI !";
		}
		else
		{
		
			$query = "SELECT id AS number from subjects WHERE name = '". $subject ."'";		
			$query = $this->db->query($query);
		
			foreach ($query->result() as $row)
    		{
    			$subjectnumber = $row->number;
        	}
		
			$wstawiane  = 'Nowa ocena';
			$numeropisu = 0;
			$description = $wstawiane;
			$flaga = 'false';
		
			while($flaga == 'false')
			{
				$query = "SELECT COUNT(d.description) AS liczba FROM classes c, students s, grades g, descriptions d, teachers t, subjects su
						WHERE c.id = s.classe_id AND g.student_id = s.id AND g.description_id = d.id AND g.teacher_id = t.id AND su.id = d.subject_id
						AND t.firstname = '" . $imie ."' AND t.secondname = '" . $nazwisko . "' AND su.name = '" 
						. $subject . "' AND c.name = '" . $class . "' AND d.description = '" . $description . "' GROUP BY d.description ORDER BY d.description ASC";
        		$query = $this->db->query($query);
        	
        		$lopisow = 0;
        		foreach ($query->result() as $resrow)
    			{	
    				$lopisow = $resrow ->liczba;			    	
        		}
        			
        			
        		if($lopisow != 0)
        		{
	        		$numeropisu++;
	        		$description = $wstawiane . " " . $numeropisu;
        		}
        		else
        		{
	        		$flaga = 'true';
        		}
    		}
    	
			$query = "INSERT into descriptions(subject_id, description, weight) values(".  $subjectnumber . ", '" . $description . "', 0)";
			$this->db->query($query);
		
			$query = "SELECT MAX(id) AS lastnumber FROM descriptions";
			$query = $this->db->query($query);
		
			foreach ($query->result() as $row)
    		{
    			$lastid = $row->lastnumber;
        	}
		
			$query = "SELECT id AS teacherid FROM teachers WHERE firstname = '" . $imie ."' AND secondname = '" . $nazwisko ."'";
			$query = $this->db->query($query);
		
			foreach ($query->result() as $row)
    		{
    			$teacherid = $row->teacherid;
        	}
        
        
        	$query = "SELECT s.id AS idnumber FROM students s, classes c WHERE s.classe_id = c.id AND c.name = '" . $class . "' ORDER BY s.secondname, s.firstname";
			$spisuczniow = $this->db->query($query);
		
			foreach ($spisuczniow->result() as $row)
    		{
	    		$query = "INSERT into grades(description_id, teacher_id, student_id, date, grade) values(" . $lastid . "," . $teacherid . "," . $row->idnumber . ", '" . date("Y-m-d") . "', 0)" ;
				$query = $this->db->query($query);	
        	}
		
        
        	$query = "SELECT COUNT(s.id) AS luczniow FROM students s, classes c WHERE s.classe_id = c.id AND c.name = '" . $class ."'";
			$query = $this->db->query($query);
		
			foreach ($query->result() as $row)
    		{
    			$luczniow = $row->luczniow;
        	}
        
        
        	$query = "SELECT COUNT(d.id) AS locen FROM classes c, subjects s, subjects_teachers st, descriptions d, teachers t
					WHERE c.id = st.classe_id AND s.id = st.subject_id AND s.id = d.subject_id AND st.teacher_id = t.id
					AND t.firstname = '" . $imie. "' AND t.secondname = '" . $nazwisko. "' AND s.name = '" . $subject . "' AND c.name = '" . $class . "'";					
			$query = $this->db->query($query);
		
			foreach ($query->result() as $row)
    		{
    			$locen = $row->locen;
        	}
        
					
			$query = "SELECT d.description AS opis, d.weight AS waga, d.id FROM classes c, students s, grades g, descriptions d, teachers t, subjects su
					WHERE c.id = s.classe_id AND g.student_id = s.id AND g.description_id = d.id AND g.teacher_id = t.id AND su.id = d.subject_id
					AND t.firstname = '" . $imie ."' AND t.secondname = '" . $nazwisko . "' AND su.name = '" . $subject . "' AND c.name = '" . $class . "' GROUP BY d.description ORDER BY d.description ASC";     
			$powody = $this->db->query($query);
					
		
			$query = "SELECT s.secondname AS nazwisko , s.firstname AS imie, d.description AS opis, g.grade AS ocena, d.weight AS waga, g.id FROM classes c, students s, grades g, descriptions d, subjects su
					WHERE c.id = s.classe_id AND s.id = g.student_id AND g.description_id = d.id AND d.subject_id = su.id AND c.name = '" . $class . "'
					AND su.name = '" . $subject . "' ORDER BY s.secondname, s.firstname, d.description";		
			$oceny = $this->db->query($query);
		
		
			$query = "SELECT s.secondname AS nazwisko, s.firstname AS imie, s.id AS id FROM students s, classes c WHERE  s.classe_id = c.id AND c.name = '" . $class . "' ORDER BY s.secondname, s.firstname";
			$spisuczniow = $this->db->query($query);
		
		
       	 	$tablica = array('przedmiot' => $subject, 'klasa' => $class, 'imie' => $imie, 'nazwisko' => $nazwisko, 'luczniow' => $luczniow, 'locen' => $locen,
        				'powody' => $powody, 'oceny' => $oceny, 'spisuczniow' => $spisuczniow);
        	$this->load->view("grades_view", $tablica);
		}
	}
	
	function submit()
	{
		$imie = $this->input->post('imie');
		$nazwisko =  $this->input->post('nazwisko');
		$subject = $this->input->post('subject');
		$class = $this->input->post('class');
		

		$query = "SELECT d.description AS opis, d.weight AS waga, d.id FROM classes c, students s, grades g, descriptions d, teachers t, subjects su
				WHERE c.id = s.classe_id AND g.student_id = s.id AND g.description_id = d.id AND g.teacher_id = t.id AND su.id = d.subject_id
				AND t.firstname = '" . $imie ."' AND t.secondname = '" . $nazwisko . "' AND su.name = '" 
				. $subject . "' AND c.name = '" . $class . "' GROUP BY d.description ORDER BY d.description ASC";    
		$powody = $this->db->query($query);
		
		
		$query = "SELECT s.secondname AS nazwisko , s.firstname AS imie, d.description AS opis, g.grade AS ocena, d.weight AS waga, g.id FROM classes c, students s, grades g, descriptions d, subjects su
					WHERE c.id = s.classe_id AND s.id = g.student_id AND g.description_id = d.id AND d.subject_id = su.id AND c.name = '" . $class . "'
					AND su.name = '" . $subject . "' ORDER BY s.secondname, s.firstname, d.description";
		$oceny = $this->db->query($query);
		
		
		$this->load->library('form_validation');
		
		foreach ($powody->result() as $row)
    	{
    		$temp = $row->id . "_" . "desdes";
    		 
			$this->form_validation->set_rules($temp, 'Za co:', 'trim|required');
        }
		 
		foreach ($powody->result() as $row)
    	{
    		$temp = $row->id . "_" . "deswei";
    		
    		$this->form_validation->set_rules($temp, 'Waga:', 'trim|required|numeric');
        }
     
		foreach ($oceny->result() as $ocrow)
    	{
    		 
    		$temp = $ocrow->id . "_" . "gra";
    		
    		$this->form_validation->set_rules($temp, 'oceny:', 'trim|required|numeric');
        }

		if ($this->form_validation->run() == FALSE)
		{
			
			$query = "SELECT COUNT(s.id) AS luczniow FROM students s, classes c WHERE s.classe_id = c.id AND c.name = '" . $class ."'";
			$query = $this->db->query($query);
		
			foreach ($query->result() as $row)
    		{
    			$luczniow = $row->luczniow;
        	}
        
        	$query = "SELECT COUNT(d.id) AS locen FROM classes c, subjects s, subjects_teachers st, descriptions d, teachers t
					WHERE c.id = st.classe_id AND s.id = st.subject_id AND s.id = d.subject_id AND st.teacher_id = t.id
					AND t.firstname = '" . $imie. "' AND t.secondname = '" . $nazwisko. "' AND s.name = '" . $subject . "' AND c.name = '" . $class . "'";			
			$query = $this->db->query($query);
		
			foreach ($query->result() as $row)
    		{
    			$locen = $row->locen;
        	}
        	
					
			$query = "SELECT d.description AS opis, d.weight AS waga, d.id FROM classes c, students s, grades g, descriptions d, teachers t, subjects su
					WHERE c.id = s.classe_id AND g.student_id = s.id AND g.description_id = d.id AND g.teacher_id = t.id AND su.id = d.subject_id
					AND t.firstname = '" . $imie ."' AND t.secondname = '" . $nazwisko . "' AND su.name = '" . $subject . "' AND c.name = '" . $class . "' GROUP BY d.description ORDER BY d.description ASC";      
			$powody = $this->db->query($query);
				
			
			$query = "SELECT s.secondname AS nazwisko , s.firstname AS imie, d.description AS opis, g.grade AS ocena, d.weight AS waga, g.id FROM classes c, students s, grades g, descriptions d, subjects su
						WHERE c.id = s.classe_id AND s.id = g.student_id AND g.description_id = d.id AND d.subject_id = su.id AND c.name = '" . $class . "'
						AND su.name = '" . $subject . "' ORDER BY s.secondname, s.firstname, d.description";
			$oceny = $this->db->query($query);
		
		
			$query = "SELECT s.secondname AS nazwisko, s.firstname AS imie, s.id AS id FROM students s, classes c WHERE  s.classe_id = c.id AND c.name = '" . $class . "' ORDER BY s.secondname, s.firstname";
			$spisuczniow = $this->db->query($query);
		
		
        	$tablica = array('przedmiot' => $subject, 'klasa' => $class, 'imie' => $imie, 'nazwisko' => $nazwisko, 'luczniow' => $luczniow, 'locen' => $locen,
        				'powody' => $powody, 'oceny' => $oceny, 'spisuczniow' => $spisuczniow);
        	$this->load->view("grades_view", $tablica);	
		}
		else
		{

			foreach ($powody->result() as $row)
    		{
    			$temp = $row->id . "_" . "desdes";
    			$description = $this->input->post($temp); 
				
    			$query = "SELECT description FROM descriptions WHERE id =" . $row -> id ;
    			$query = $this->db->query($query);
    		
    			foreach ($query->result() as $powrow)
    			{
    				$olddescription = $powrow ->description;			    	
        		}
        	
        		if($olddescription != $description)
        		{
	        		$flaga = 'false';
	        		$numerporzadkowy = 0;
	        		$descriptionn = $description;
	        		while($flaga == 'false')
	        		{
		        		$query = "SELECT COUNT(d.description) AS liczba FROM classes c, students s, grades g, descriptions d, teachers t, subjects su
							 	WHERE c.id = s.classe_id AND g.student_id = s.id AND g.description_id = d.id AND g.teacher_id = t.id AND su.id = d.subject_id
								AND t.firstname = '" . $imie ."' AND t.secondname = '" . $nazwisko . "' AND su.name = '" 
								. $subject . "' AND c.name = '" . $class . "' AND d.description = '" . $descriptionn . "' GROUP BY d.description ORDER BY d.description ASC";
        				$query = $this->db->query($query);
        			
        				$lopisow = 0;
        				foreach ($query->result() as $resrow)
    					{	
    						$lopisow = $resrow ->liczba;			    	
        				}
        			
        			
        				if($lopisow != 0)
        				{
	        				$numerporzadkowy++;
	        				$descriptionn = $description . " " . $numerporzadkowy;
        				}
        				else
        				{
	        				$flaga = 'true';
        				}
	        		}
	        	
	        	
	        		$query = "UPDATE descriptions SET description = '" . $descriptionn . "' WHERE id =" . $row->id;
	        		$query = $this->db->query($query);
        		}
    		
	    	
        	}
        
        
        
        	foreach ($powody->result() as $row)
    		{
    			$temp = $row->id . "_" . "deswei";
    			$weight = $this->input->post($temp); 
				
    			$query = "SELECT weight FROM descriptions WHERE id =" . $row -> id ;
    			$query = $this->db->query($query);
    			
    			foreach ($query->result() as $powrow)
    			{
    				$oldweight = $powrow -> weight;			    	
        		}
        	
        
        		if($oldweight != $weight)
        		{
	        		$query = "UPDATE descriptions SET weight = '" . $weight . "' WHERE id =" . $row->id;
	        		$query = $this->db->query($query);
        		}
        	}
        
        	foreach ($oceny->result() as $ocrow)
    		{
    		 
    			$temp = $ocrow->id . "_" . "gra";
    			$grade = $this->input->post($temp); 
    		
    			$query = "SELECT grade FROM grades WHERE id =" . $ocrow -> id ;
    			$query = $this->db->query($query);
	    	
    			foreach ($query->result() as $grarow)
    			{
    				$oldgrade = $grarow ->grade;			    	
        		}
        	
    			if($oldgrade != $grade)
    			{
	    			$query = "UPDATE grades SET grade = '" . $grade . "', date = '" . date("Y-m-d") . "' WHERE id =" . $ocrow->id;
	        		$query = $this->db->query($query);
    			}
        	}
        
        	$query = "SELECT COUNT(s.id) AS luczniow FROM students s, classes c WHERE s.classe_id = c.id AND c.name = '" . $class ."'";
			$query = $this->db->query($query);
		
			foreach ($query->result() as $row)
    		{
    			$luczniow = $row->luczniow;
        	}
        
        	$query = "SELECT COUNT(d.id) AS locen FROM classes c, subjects s, subjects_teachers st, descriptions d, teachers t
						WHERE c.id = st.classe_id AND s.id = st.subject_id AND s.id = d.subject_id AND st.teacher_id = t.id
						AND t.firstname = '" . $imie. "' AND t.secondname = '" . $nazwisko. "' AND s.name = '" . $subject . "' AND c.name = '" . $class . "'";			
			$query = $this->db->query($query);
		
			foreach ($query->result() as $row)
    		{
    			$locen = $row->locen;
        	}
        
					
			$query = "SELECT d.description AS opis, d.weight AS waga, d.id FROM classes c, students s, grades g, descriptions d, teachers t, subjects su
					WHERE c.id = s.classe_id AND g.student_id = s.id AND g.description_id = d.id AND g.teacher_id = t.id AND su.id = d.subject_id
					AND t.firstname = '" . $imie ."' AND t.secondname = '" . $nazwisko . "' AND su.name = '" . $subject . "' AND c.name = '" . $class . "' GROUP BY d.description ORDER BY d.description ASC";      
			$powody = $this->db->query($query);
				
			
			$query = "SELECT s.secondname AS nazwisko , s.firstname AS imie, d.description AS opis, g.grade AS ocena, d.weight AS waga, g.id FROM classes c, students s, grades g, descriptions d, subjects su
						WHERE c.id = s.classe_id AND s.id = g.student_id AND g.description_id = d.id AND d.subject_id = su.id AND c.name = '" . $class . "'
						AND su.name = '" . $subject . "' ORDER BY s.secondname, s.firstname, d.description";
			$oceny = $this->db->query($query);
		
		
			$query = "SELECT s.secondname AS nazwisko, s.firstname AS imie, s.id AS id FROM students s, classes c WHERE  s.classe_id = c.id AND c.name = '" . $class . "' ORDER BY s.secondname, s.firstname";
			$spisuczniow = $this->db->query($query);
		
		
        	$tablica = array('przedmiot' => $subject, 'klasa' => $class, 'imie' => $imie, 'nazwisko' => $nazwisko, 'luczniow' => $luczniow, 'locen' => $locen,
        				'powody' => $powody, 'oceny' => $oceny, 'spisuczniow' => $spisuczniow);
        	$this->load->view("grades_view", $tablica);
		}
	}
	
		function usunzaznaczone()
		{
			echo "aaa";
			$imie = $this->input->post('imie');
			$nazwisko =  $this->input->post('nazwisko');
			$subject = $this->input->post('subject');
			$class = $this->input->post('class');
				
		
			$dousuniecia = $this->input->post('dousuniecia');
			
			if($dousuniecia != NULL)
			{
				foreach($dousuniecia as $opis)
 				{
 					$query = "SELECT d.id as id FROM classes c, students s, grades g, descriptions d, subjects su, teachers t WHERE c.id = s.classe_id AND g.description_id = d.id 
 							AND d.subject_id = su.id AND g.teacher_id = t.id AND s.id = g.student_id AND c.name = '" . $class . "' AND t.firstname = '" . $imie . "' AND 
 						t.secondname = '" . $nazwisko ."' AND su.name = '" . $subject . "' AND d.description = '" . $opis . "' GROUP BY d.id";
 				
 					$idopisu = $this->db->query($query);
 				
 					foreach ($idopisu->result() as $row)
    				{
    					$query = "DELETE FROM grades WHERE description_id =" . $row->id; 
    					$query = $this->db->query($query);
    					$query = "DELETE FROM descriptions WHERE id =" . $row->id;
    					$query = $this->db->query($query);
        			}
 				}
			}
		
		
			$query = "SELECT COUNT(s.id) AS luczniow FROM students s, classes c WHERE s.classe_id = c.id AND c.name = '" . $class ."'";
			$query = $this->db->query($query);
		
			foreach ($query->result() as $row)
    		{
    			$luczniow = $row->luczniow;
        	}
        
        
        	$query = "SELECT COUNT(d.id) AS locen FROM classes c, subjects s, subjects_teachers st, descriptions d, teachers t
					WHERE c.id = st.classe_id AND s.id = st.subject_id AND s.id = d.subject_id AND st.teacher_id = t.id
					AND t.firstname = '" . $imie. "' AND t.secondname = '" . $nazwisko. "' AND s.name = '" . $subject . "' AND c.name = '" . $class . "'";			
			$query = $this->db->query($query);
		
			foreach ($query->result() as $row)
    		{
    			$locen = $row->locen;
        	}
		
					
			$query = "SELECT d.description AS opis, d.weight AS waga, d.id FROM classes c, students s, grades g, descriptions d, teachers t, subjects su
					WHERE c.id = s.classe_id AND g.student_id = s.id AND g.description_id = d.id AND g.teacher_id = t.id AND su.id = d.subject_id
					AND t.firstname = '" . $imie ."' AND t.secondname = '" . $nazwisko . "' AND su.name = '" . $subject . "' AND c.name = '" . $class . "' GROUP BY d.id ORDER BY d.description ASC";
			$powody = $this->db->query($query);
				
			
			$query = "SELECT s.secondname AS nazwisko , s.firstname AS imie, d.description AS opis, g.grade AS ocena, d.weight AS waga, g.id FROM classes c, students s, grades g, descriptions d, subjects su
						WHERE c.id = s.classe_id AND s.id = g.student_id AND g.description_id = d.id AND d.subject_id = su.id AND c.name = '" . $class . "'
						AND su.name = '" . $subject . "' ORDER BY s.secondname, s.firstname, d.description";
			$oceny = $this->db->query($query);
		
		
			$query = "SELECT s.secondname AS nazwisko, s.firstname AS imie, s.id AS id FROM students s, classes c WHERE  s.classe_id = c.id AND c.name = '" . $class . "' ORDER BY s.secondname, s.firstname";
			$spisuczniow = $this->db->query($query);
		
		
		
        	$tablica = array('przedmiot' => $subject, 'klasa' => $class, 'imie' => $imie, 'nazwisko' => $nazwisko, 'luczniow' => $luczniow, 'locen' => $locen,
        				'powody' => $powody, 'oceny' => $oceny, 'spisuczniow' => $spisuczniow);
        	$this->load->view("grades_view", $tablica);	 
		}
	//}
}

?>